# coding=utf-8
import hashlib
import time
import json
import requests


class Crontab(object):
    """
    """

    def __init__(self):
        """
        """
        # DEV
        # self.host = "http://54.191.225.93"
        # self.host = "http://dev.alive-story.com"
        # self.host = "https://devs.alive-story.com"

        # PROD
        # self.host = "http://52.11.238.47"  # prod-app-server-01
        # self.host = "http://18.236.82.229"  # prod-app-server-02
        # self.host = "http://prod.alive-story.com"
        self.host = "https://prods.alive-story.com"

        self.port = 20000
        self.uri = '/internal/v1/'
        self.url = '%s:%s%s' % (self.host, self.port, self.uri)
        self.app_version = '6.6'
        self.secretKey = 'alive_2018'

    def run(self):
        """
        """

        cmdid = 20000

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'md5',
            "RequestSign": 'kkkkkk',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 'xxxx',
                'cmdId': cmdid,
                'release': 1,
                'platform': 2
            },
            "params": {
                'param1': '111'
            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=md5&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))


if __name__ == '__main__':
    Crontab().run()
